home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.1v2 SDK / Sample Code / MPPeriodicalTest ƒ / MPPeriodicalTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  7.4 KB  |  215 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        "MPPeriodicalTest.c"
  3.     
  4.     Description:
  5.                 This is a application to test the MT/MP timers.
  6.  
  7.     Version:    v0.0
  8.  
  9.     File Ownership:
  10.  
  11.         DRI:                George Warner
  12.  
  13.         Other Contact:        
  14.  
  15.         Technology:            MultiTasking/MultiProcessing
  16.  
  17.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  18.     
  19.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  20.                 ("Apple") in consideration of your agreement to the following terms, and your
  21.                 use, installation, modification or redistribution of this Apple software
  22.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  23.                 please do not use, install, modify or redistribute this Apple software.
  24.  
  25.                 In consideration of your agreement to abide by the following terms, and subject
  26.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  27.                 copyrights in this original Apple software (the "Apple Software"), to use,
  28.                 reproduce, modify and redistribute the Apple Software, with or without
  29.                 modifications, in source and/or binary forms; provided that if you redistribute
  30.                 the Apple Software in its entirety and without modifications, you must retain
  31.                 this notice and the following text and disclaimers in all such redistributions of
  32.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  33.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  34.                 Apple Software without specific prior written permission from Apple.  Except as
  35.                 expressly stated in this notice, no other rights or licenses, express or implied,
  36.                 are granted by Apple herein, including but not limited to any patent rights that
  37.                 may be infringed by your derivative works or by other works in which the Apple
  38.                 Software may be incorporated.
  39.  
  40.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  41.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  42.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  44.                 COMBINATION WITH YOUR PRODUCTS.
  45.  
  46.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  47.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  48.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  50.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  51.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  52.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53.  
  54.     Writers:
  55.  
  56.         gaw    - George Warner
  57.  
  58.     Change History (most recent first):
  59.  
  60.          <1>    02/29/00    gaw     Initial build
  61.  
  62.     Author Initials:
  63.  
  64.     gaw - George Warner
  65.  
  66. */
  67.  
  68. #pragma mark compiler directives
  69. #define USE_ABSOLUTE_TIMER 0    // set to 0 to use duration timer
  70.  
  71. #pragma mark #Includes
  72. #include <MacTypes.h>
  73. #include <Events.h>
  74. #include <Fonts.h>
  75. #include <Multiprocessing.h>
  76.  
  77. #include <stdio.h>            // for printf & fflush
  78. #include <SIOUX.h>            // for SIOUXSettings stuff
  79. #include <SIOUXGlobals.h>    // for SIOUXQuitting
  80.  
  81. #pragma mark typedefs, structs, enums, defines, etc.
  82. #define kNumCounts 20    // This is the number of times we count down
  83.  
  84. #pragma mark exported functions
  85. void main(void)
  86. {
  87.     MPTimerID        tMPTimerID;
  88.     MPSemaphoreID    tMPSemaphoreID;
  89.     AbsoluteTime     expirationTime;
  90.     Duration        timeout = 1 * durationSecond;
  91.     OSStatus        anErr;
  92.     SInt32            tCount = kNumCounts;
  93.  
  94.     // Set the SIOUX window defaults
  95.     SIOUXSettings.autocloseonquit    = false;
  96.     SIOUXSettings.asktosaveonclose    = false;
  97.     SIOUXSettings.showstatusline    = false;
  98.     SIOUXSettings.columns            = 132;
  99.     SIOUXSettings.rows                = 24;
  100.     SIOUXSettings.fontsize            = 10;
  101.     GetFNum("\pMonaco",&SIOUXSettings.fontid);
  102.     SIOUXSettings.standalone        = true;
  103.  
  104.     printf("MPPeriodicalTest Starting!\n");
  105.  
  106.     // Make sure that the MP library is loaded
  107.     if    (!MPLibraryIsLoaded())
  108.     {
  109.         printf("The MP library did not load.\n");
  110.         return;
  111.     }
  112.  
  113.     {    // for giggles we'll dump out the MP Library version info
  114.         const char* tVersionCStringPtr;
  115.         UInt32 major,minor,release,revision;
  116.  
  117.         _MPLibraryVersion(&tVersionCStringPtr,&major,&minor,&release,&revision);
  118.  
  119.         printf("\n Version: \"%s\", major: %ld, minor: %ld, release: %ld, revision: %ld.\n",tVersionCStringPtr,major,minor,release,revision);
  120.     }
  121.  
  122.     // Now create a timer
  123.     anErr = MPCreateTimer(&tMPTimerID);
  124.     if (noErr != anErr)
  125.     {
  126.         printf("MPCreateTimer error: %ld.\n", anErr);
  127.         return;
  128.     }
  129.  
  130.     // and a semaphore
  131.     anErr = MPCreateSemaphore(kNumCounts,0,&tMPSemaphoreID);
  132.     if (noErr != anErr)
  133.     {
  134.         printf("MPCreateSemaphore error: %ld.\n", anErr);
  135.         return;
  136.     }
  137.  
  138.     // Link the timer to the semaphore so that when the timer
  139.     // fires the semaphore will be notified
  140.     anErr = MPSetTimerNotify(tMPTimerID,(MPOpaqueID) tMPSemaphoreID,nil,nil,nil);
  141.     if (noErr != anErr)
  142.     {
  143.         printf("MPSetTimerNotify error: %ld.\n", anErr);
  144.         return;
  145.     }
  146.  
  147. #if USE_ABSOLUTE_TIMER
  148.     // calculate the expiration time based on the current
  149.     // absolute time plus a duration and arm the timer.
  150.     // The kMPPreserveTimerIDMask flag tells MP that we will be reusing the timer
  151.     expirationTime = UpTime();
  152.     expirationTime = AddDurationToAbsolute(timeout,expirationTime);
  153.     anErr = MPArmTimer(tMPTimerID,&expirationTime,kMPPreserveTimerIDMask);
  154. #else // use duration
  155.     // Pass the duration by address and set the kMPTimeIsDurationMask flag.
  156.     // Set the kMPTimeIsDeltaMask flag tell MP to add this delta to the 
  157.     // last time the timer fired (or in this case, when the timer was created).
  158.     // The kMPPreserveTimerIDMask flag tells MP that we will be reusing the timer
  159.     anErr = MPArmTimer(tMPTimerID,(AbsoluteTime*) &timeout,
  160.                 kMPPreserveTimerIDMask | kMPTimeIsDeltaMask | kMPTimeIsDurationMask);
  161. #endif
  162.     if (noErr != anErr)
  163.         printf("MPArmTimer error: %ld.\n", anErr);
  164.  
  165.     printf("Waiting %ld seconds...\n",tCount);
  166.  
  167.     while ((tCount > 0) && (!SIOUXQuitting))
  168.     {
  169.         // it's importaint that our WaitNextEvent loop not be CPU bound or
  170.         // blocked by the MPWaitOnSemaphore so we use kDurationImmediate
  171.         // to poll the semaphore and use a sleep time of one with WNE.
  172.         if (kMPTimeoutErr == MPWaitOnSemaphore(tMPSemaphoreID,kDurationImmediate))
  173.         {
  174.             EventRecord theEvent;
  175.             WaitNextEvent(everyEvent,&theEvent,1,nil);
  176.             SIOUXHandleOneEvent(&theEvent);    // tell SIOUX to handle the event
  177.         }
  178.         else
  179.         {
  180. #if USE_ABSOLUTE_TIMER
  181.             // calculate the expiration time based on the last absolute
  182.             // time plus our duration and then arm the timer again.
  183.             expirationTime = AddDurationToAbsolute(timeout,expirationTime);
  184.             anErr = MPArmTimer(tMPTimerID,&expirationTime,kMPPreserveTimerIDMask);
  185. #else    // use duration
  186.             // Pass the duration by address and set the kMPTimeIsDurationMask flag.
  187.             // Set the kMPTimeIsDeltaMask flag tell MP to add this delta 
  188.             // to the last time the timer fired.
  189.             anErr = MPArmTimer(tMPTimerID,(AbsoluteTime*) &timeout,
  190.                         kMPPreserveTimerIDMask | kMPTimeIsDeltaMask | kMPTimeIsDurationMask);
  191. #endif
  192.             if (noErr != anErr)
  193.                 printf("MPArmTimer error: %ld.\n", anErr);
  194.  
  195.             printf("%d ",--tCount);
  196.             fflush(stdout);
  197.         }
  198.     }
  199.  
  200.     // cancel the timer
  201.     anErr = MPCancelTimer(tMPTimerID,&expirationTime);
  202.     if (noErr != anErr)
  203.         printf("MPCancelTimer error: %d.\n",anErr);
  204.  
  205.     // dispose of our timer
  206.     anErr = MPDeleteTimer(tMPTimerID);
  207.     if (noErr != anErr)
  208.         printf("MPDeleteTimer error: %d.\n",anErr);
  209.  
  210.     // dispose of our semaphore
  211.     MPDeleteSemaphore(tMPSemaphoreID);
  212.  
  213.     printf("Done!\nPress command-Q to quit.\n");
  214. }
  215.